//
//  HttpUtil.m
//  OnlineExam
//
//  Created by Goven on 13-10-18.
//  Copyright (c) 2013年 Goven. All rights reserved.
//

#import "HttpUtil.h"
#import "AFNetworking.h"


@implementation HttpUtil

+ (void)doGetWithUrl:(NSString *)url path:(NSString *)path params:(NSDictionary *)params callback:(BKHttpCallback) callback
{
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:url]];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager GET:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            callback(YES, responseObject);
        }else
        {
            callback(NO, nil);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        LOG(@"%@",error.localizedDescription)
        callback(NO, nil);
    }];
}

+ (void)doPostWithUrl:(NSString *)url path:(NSString *)path params:(NSDictionary *)params callback:(BKHttpCallback)callback
{
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:url]];
    manager.securityPolicy.allowInvalidCertificates = YES;
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (responseObject)
        {
           
            callback(YES, responseObject);
        }
        else
        {
            callback(NO, nil);
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         callback(NO, nil);
    }];
}

//+ (void)getImageWithUrl:(NSString *)url callback:(BKHttpCallback)callback
//{
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
//    AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:request];
//    
//    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        UIImage *image = responseObject;
//        NSDictionary *result = @{@"image":image};
//        callback(YES, result);
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        callback(NO, nil);
//    }];
//    [requestOperation start];
//}

@end
